const SMSClient = require('@alicloud/sms-sdk')
const http = require('http');
const iconv = require('iconv-lite');
const cheerio = require('cheerio');
const async = require('async');

const teml = {
    morning: 'SMS_135043350'
};

async.auto({
    spiderData: function (callback) {
       http.get('http://tianqi.2345.com/shenzhen/59493.htm',function(req,res){
           let  html = ''
           req.on('data',function(chunk){
               html += iconv.decode(chunk, 'GBK')
           });
           req.on('end',function(){
               const $ = cheerio.load(html);
               console.log($('a.data').text())
               callback(null, $('a.data').text())
           });
       });
    },
    sendMsg: ['spiderData', function (results) {
        console.log(results)
        let templateParam = {
            name: '今天' + results.spiderData};
        const cofig = {
            accessKeyId: 'LTAIfRcWeJgZ2dy2',
            secretAccessKey: 'AIunlATW7DdIOQzv8C14dVkxbRpcHR'
        }
//初始化sms_client
        let smsClient = new SMSClient(cofig)
//发送短信
        smsClient.sendSMS({
	    PhoneNumbers: '17688891552',
            SignName: '静静',
            TemplateCode: teml.morning,
            TemplateParam: JSON.stringify(templateParam)
        }).then(function (res) {
            let {Code}=res;
            if (Code === 'OK') {
                //处理返回参数
                console.log(res)
            }
        }, function (err) {
            console.log(err)
        });
    }]
});
